package com.heba.adapter.diary;

import android.content.Context;
import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.heba.bean.diary.ItemUploadPhotoObject;
import com.heba.greenmulu.R;

import java.io.File;
import java.util.List;

/**
 * 日記圖片大圖顯示
 * <p>
 * Created by xingbao on 2019/2/12.
 */

public class DiaryPhotoEditPagerAdapter extends PagerAdapter {

    private Context context;
    private List<ItemUploadPhotoObject> list;

    private DiaryPhotoEditListener listener;

    public DiaryPhotoEditPagerAdapter(Context context, List<ItemUploadPhotoObject> list) {
        this.context = context;
        this.list = list;
    }

    /**
     * 设置监听接口
     *
     * @param listener
     */
    public void setonDiaryPhotoEditListener(DiaryPhotoEditListener listener) {
        this.listener = listener;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        return arg0 == arg1;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

    @Override
    public int getItemPosition(@NonNull Object object) {
        return POSITION_NONE;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_diary_photo_edit, null);
        ImageView iv_photo = view.findViewById(R.id.iv_photo);
        ImageView iv_edit = view.findViewById(R.id.iv_edit);

        ItemUploadPhotoObject object = list.get(position);
        Glide.with(context).load(new File(object.getPhotoPath())).into(iv_photo);

        /**
         * 编辑
         */
        iv_edit.setOnClickListener(v -> {
            if (listener != null) {
                listener.onPhotoEdit(position, object.getPhotoPath());
            }
        });

        container.addView(view);
        return view;
    }

    /**
     * 刷新数据
     *
     * @param list
     */
    public void setList(List<ItemUploadPhotoObject> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    /**
     * 图片编辑接口
     */
    public interface DiaryPhotoEditListener {

        void onPhotoEdit(int position, String url);

    }

}
